package com.example.gbuffer;

import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.opengl.GLSurfaceView;
import com.example.gbuffer.Utils.OpenGLUtils;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.util.Log;
import android.view.MotionEvent;

public class MainActivity extends AppCompatActivity {

    private float mLastX = 0;
    private float mLastY = 0;
    private float mSensitivity = 0.2f;

    private GLSurfaceView mGLSurfaceView;
    private GLRender mGLRenderer;

    private final String LOG_TAG = "Activity";

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.i(LOG_TAG, "------- onCreate ----------");
        Log.d(LOG_TAG, "support opengl es 3.2 ? " + OpenGLUtils.isOpenGLES32Supported(this));

        // 设置Activity为横屏显示
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        // 实例化GLSurfaceView (EGL处理opengl es 与窗口系统）
        mGLSurfaceView = new GLSurfaceView(this);
        mGLSurfaceView.setEGLContextClientVersion(3);

        // 设置渲染器
        mGLRenderer = new GLRender(this);
        mGLSurfaceView.setRenderer(mGLRenderer);

        setContentView(mGLSurfaceView);

        mGLSurfaceView.setOnTouchListener((v, event) -> {
            switch(event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    // 当按下时的处理
                    this.mLastX = event.getRawX();
                    this.mLastY = event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    // 当滑动时的处理
                {
//                    float deltaX = mSensitivity * (event.getRawX() - mLastX);
//                    float deltaY = mSensitivity * (event.getRawY() - mLastY);
//                    mGLRenderer.yrot += deltaX;
//                    mGLRenderer.xrot += deltaY;
//
//                    mLastX = event.getRawX();
//                    mLastY = event.getRawY();
                }
                break;
                case MotionEvent.ACTION_UP:
                    // 当抬起时的处理
                    break;
            }
            return true;
        });
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        mGLSurfaceView.onResume();
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        mGLSurfaceView.onPause();
    }
}